package com.example.mapper;

import com.example.constant.GoodsState;
import com.example.pojo.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {
    // 查询商品列表（可按分类和状态筛选）
    @Select("<script>" +
            "SELECT g.*, s.name as stuName, gc.name as cateName " +
            "FROM goods g " +
            "LEFT JOIN student s ON g.stu_id = s.id " +
            "LEFT JOIN `goods-cate` gc ON g.cate_id = gc.id " +
            "<where>" +
            "   <if test='name != null'>AND g.name LIKE CONCAT('%', #{name}, '%')</if>" +
            "   <if test='cateId != null'>AND g.cate_id = #{cateId}</if>" +
            "   <if test='stuName != null'>AND s.name LIKE CONCAT('%', #{stuName}, '%')</if>" +
            "   <if test='state != null'>AND g.state = #{state}</if>" +
            "</where>" +
            "ORDER BY g.create_time DESC" +
            "</script>")
    List<Goods> list(@Param("name") String name,
                     @Param("cateId") Integer cateId,
                     @Param("stuName") String stuName,
                     @Param("state") String state);

    // 查询商品详情
    @Select("SELECT * FROM goods WHERE id = #{id}")
    Goods findById(Integer id);

    // 更新商品信息（主要用于状态更新）
    @Update("UPDATE goods SET " +
            "state = #{state}, " +
            "update_time = #{updateTime} " +
            "WHERE id = #{id}")
    void update(Goods goods);


}